package com.cmc.cloud.cmclink.doc.service.impl.so;

import com.cmc.cloud.cmclink.doc.api.document.emums.BasicConfigEnum;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.BasicConfigErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.BasicConfigConvert;
import com.cmc.cloud.cmclink.doc.entity.BasicConfigDO;
import com.cmc.cloud.cmclink.doc.mapper.BasicConfigMapper;
import com.cmc.cloud.cmclink.doc.service.so.BasicConfigService;
import com.cmc.cloud.cmclink.doc.vo.sovo.basicconfgvo.*;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmes.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * 用户自定义信息 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class BasicConfigServiceImpl implements BasicConfigService {

    @Resource
    private BasicConfigMapper basicConfigMapper;

    @Override
    public void createBasicConfig(BasicConfigCreateReqVO createReqVO) {
        // 插入
        validateUnique(createReqVO, null);
        BasicConfigDO basicConfig = BasicConfigConvert.INSTANCE.convert(createReqVO);
        basicConfigMapper.insert(basicConfig);
    }

    private void validateUnique(BasicConfigBaseVO vo, Long id) {
        LambdaQueryWrapperX<BasicConfigDO> queryWrapperX = new LambdaQueryWrapperX<BasicConfigDO>()
            .eq(BasicConfigDO::getCode, vo.getCode())
            .eq(BasicConfigDO::getCodeDefine, vo.getCodeDefine())
            .eq(BasicConfigDO::getPortCd, vo.getPortCd());
        if (id != null) {
            queryWrapperX.ne(BasicConfigDO::getId, id);
        }
        if (basicConfigMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(BasicConfigErrorCodeConstants.BASIC_CONFIG_EXISTS);
        }
    }

    @Override
    public void updateBasicConfig(BasicConfigUpdateReqVO updateReqVO) {
        // 校验存在
        validateBasicConfigExists(updateReqVO.getId());
        validateUnique(updateReqVO, updateReqVO.getId());
        // 更新
        BasicConfigDO updateObj = BasicConfigConvert.INSTANCE.convert(updateReqVO);
        basicConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicConfig(Long id) {
        // 校验存在
        validateBasicConfigExists(id);
        // 删除
        basicConfigMapper.deleteById(id);
    }

    @Override
    public void deleteBatchBasicConfig(Collection<Long> ids) {
        basicConfigMapper.deleteBatchIds(ids);
    }

    private void validateBasicConfigExists(Long id) {
        if (basicConfigMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(BasicConfigErrorCodeConstants.BASIC_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public BasicConfigDO getBasicConfig(Long id) {
        return basicConfigMapper.selectById(id);
    }

    @Override
    public List<BasicConfigDO> getBasicConfigList(Collection<Long> ids) {
        return basicConfigMapper.selectBatchIds(ids);
    }

    @Override
    public List<String> getBasicConfigCodeDefineList() {
        return basicConfigMapper.getBasicConfigCodeDefineList();
    }

    @Override
    public PageResult<BasicConfigDO> getBasicConfigPage(BasicConfigPageReqVO reqVO) {
        LambdaQueryWrapperX<BasicConfigDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(BasicConfigDO::getCodeDefine, reqVO.getCodeDefine())
                .eqIfPresent(BasicConfigDO::getPortCd, reqVO.getPortCd())
                .likeIfPresent(BasicConfigDO::getCode, reqVO.getCode());
        return basicConfigMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public void createJpBasicConfig(BasicConfigJpBaseVO createReqVO) {
        //校验是否存在相同的数据
        LambdaQueryWrapperX<BasicConfigDO> basicConfigDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        basicConfigDoLambdaQueryWrapperX
                .eq(BasicConfigDO::getPortCd, createReqVO.getPortCd())
                .eq(BasicConfigDO::getLaneCode, createReqVO.getLaneCode())
                .eq(BasicConfigDO::getVesselOperatorCode, createReqVO.getVesselOperatorCode())
                .eq(BasicConfigDO::getCodeDefine, BasicConfigEnum.AFROPERATORCODE.getValue());
        Long count = basicConfigMapper.selectCount(basicConfigDoLambdaQueryWrapperX);
        if (0L < count) {
            throw ServiceExceptionUtil.exception(BasicConfigErrorCodeConstants.BASIC_CONFIG_EXISTS);
        }
        // 插入
        BasicConfigDO basicConfig = BasicConfigConvert.INSTANCE.convertBasicConfigDO(createReqVO);
        basicConfigMapper.insert(basicConfig);
    }

    @Override
    public void updteJpBasicConfig(BasicConfigJpUpdateReqVO basicConfigJpUpdateReqVO) {
        // 校验存在
        validateBasicConfigExists(basicConfigJpUpdateReqVO.getId());
        //校验是否存在相同的数据
        LambdaQueryWrapperX<BasicConfigDO> basicConfigDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        basicConfigDoLambdaQueryWrapperX
                .ne(BasicConfigDO::getId, basicConfigJpUpdateReqVO.getId())
                .eq(BasicConfigDO::getPortCd, basicConfigJpUpdateReqVO.getPortCd())
                .eq(BasicConfigDO::getLaneCode, basicConfigJpUpdateReqVO.getLaneCode())
                .eq(BasicConfigDO::getVesselOperatorCode, basicConfigJpUpdateReqVO.getVesselOperatorCode())
                .eq(BasicConfigDO::getCodeDefine, BasicConfigEnum.AFROPERATORCODE.getValue());
        Long count = basicConfigMapper.selectCount(basicConfigDoLambdaQueryWrapperX);
        if (0L < count) {
            throw ServiceExceptionUtil.exception(BasicConfigErrorCodeConstants.BASIC_CONFIG_EXISTS);
        }

        BasicConfigDO basicConfigDO = BasicConfigConvert.INSTANCE.convertBasicConfigJpUpdateReqVO(basicConfigJpUpdateReqVO);
        basicConfigDO.setUpdateTime(LocalDateTime.now());
        basicConfigDO.setUpdater(String.valueOf(SecurityFrameworkUtils.getLoginUser().getId()));
        basicConfigMapper.updateById(basicConfigDO);
    }
}
